<template>
  <div>
    <h1>欢迎登录</h1>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="login-box">
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('form')">登录</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "login",
    data() {
      return{
        form:{
          username: '',
          password:''
        },
        rules:{
          username:[
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ],
          password:[
            { required: true, message: '请输入活动名称', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      login(form) {
        this.$refs[form].validate((valid) => {
          if (valid) {
            //sessionStorage只能放字符串
            sessionStorage.setItem("isLogin","true")
            //调用action方法执行更新
            this.$store.dispatch("asyncUpdateUser",this.form)
            //路由跳转
            this.$router.push('/main')
          } else {
            this.$message({
              showClose: true,
              duration: 1000,
              message: '错了哦，这是一条错误消息',
              type: 'error'
            });
            return false;
          }
        });
      }
    }
  }
</script>

<style scoped>

  .login-box{
    width: 400px;
    margin: 0 auto;
    border: 1px solid #DCDFE6;
    padding: 40px 50px 20px 20px;
    text-align: left;
    border-radius: 5px;
    box-shadow: 0 0 20px;
  }

</style>
